package com.module.overview.ui.cardHistoryDetails.provider

import com.chad.library.adapter.base.provider.BaseItemProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.common.app.data.bean.overView.HistoryDetailsModel
import com.common.app.utls.TimeUtils
import com.common.base.app.extras.doOnIOThread
import com.common.base.app.extras.doOnUiThread
import com.common.base.utils.AppUtils.getString
import com.common.base.utils.JsonUtils
import com.common.base.utils.MathUtil
import com.module.ble.data.bean.CusChartData
import com.module.ble.db.table.UserAcLevel
import com.module.ble.repo.UserAcLevelRepo
import com.module.ble.utils.AllCardTypeConfig
import com.module.ble.utils.common48DataConvert
import com.module.ble.widget.AcStrengthBarChart
import com.module.overview.R

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2025/7/14
 *
 * 描述：活动-强度卡片
 *
 * 修订历史：完成度100%
 *
 */
class OvActivityStrengthCardProvider(time:Long,
    override val itemViewType: Int = AllCardTypeConfig.TYPE_CARD_ACTIVITY_DAY_STRENGTH,
    override val layoutId: Int = R.layout.overview_activity_strength_crad_provider
) : BaseItemProvider<HistoryDetailsModel>() {
    private var curTime = time

    private var candlestickChart: AcStrengthBarChart? = null

    init {
    }

    override fun convert(helper: BaseViewHolder, item: HistoryDetailsModel) {
        // 设置Y轴阶段
        candlestickChart?.setYAxisStages(
            floatArrayOf(0f, 30f,60f, 80f, 100f), // 阶段分界点
        )

//        candlestickChart?.setXAxisSolidLineEnabled(true)

        getTheDayAcLevelData()

    }


    override fun onViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
        candlestickChart = viewHolder.getView(R.id.candlestickChart)
        candlestickChart?.setBarRounding(enableTop = true, enableBottom = false)

        // 设置触摸监听
        candlestickChart?.setOnBarTouchListener(object : AcStrengthBarChart.OnBarTouchListener {
            override fun onBarTouched(barIndex: Int, minValue: Float, maxValue: Float, timeLabel: String) {
                // 处理触摸事件
//                Log.d("CandlestickChart", "触摸到柱子: 索引=$barIndex,触摸到: $timeLabel, 值: $minValue")
                viewHolder.setText(R.id.tvAcLevel,when {
                    maxValue >= 80f -> getString(R.string.common_strength_high_hint) // 80-100: 红色
                    maxValue >= 60f ->getString(R.string.common_strength_medium_hint) // 60-80: 橙色
                    maxValue >= 30f -> getString(R.string.common_strength_low_hint) // 30-60: 黄色
                    else -> getString(R.string.common_strength_inactive_hint) // 0-30: 浅蓝色
                })

                candlestickChart?.let {
                    viewHolder.setText(R.id.tAcLevelUnit,"${TimeUtils.getHourMinute(it.getData()[barIndex].createdTime)}-${TimeUtils.getHourMinute(it.getData()[barIndex].endTime)}")
                }

                viewHolder.setVisible(R.id.tvAcLevel, true)
                viewHolder.setVisible(R.id.tAcLevelUnit, true)
            }

            override fun onTouchReleased() {
                // 处理触摸释放事件
                viewHolder.setVisible(R.id.tvAcLevel, false)
                viewHolder.setVisible(R.id.tAcLevelUnit, false)
            }
        })

        super.onViewHolderCreated(viewHolder, viewType)
    }

    /**
     * 获取当天活动强度信息
     */
    fun getTheDayAcLevelData(){
        if (curTime <=0) return

        doOnIOThread {
            UserAcLevelRepo.instance.findByTimestampTheDay(curTime)?.let { originalListIt->
                //测试数据
//                val originalListIt :List<UserAcLevel> = JsonUtils.fromJsonList("[{\"createdTime\":1755878460000,\"endTime\":0,\"firmName\":\"2\",\"inDateTime\":1755878460000,\"acLevel\":73},{\"createdTime\":1755879240000,\"endTime\":0,\"firmName\":\"2\",\"inDateTime\":1755879240000,\"acLevel\":80},{\"createdTime\":1755879300000,\"endTime\":0,\"firmName\":\"2\",\"inDateTime\":1755879300000,\"acLevel\":43},{\"createdTime\":1755879360000,\"endTime\":0,\"firmName\":\"2\",\"inDateTime\":1755879360000,\"acLevel\":46},{\"createdTime\":1755883020000,\"endTime\":0,\"firmName\":\"2\",\"inDateTime\":1755883020000,\"acLevel\":51},{\"createdTime\":1755883080000,\"endTime\":0,\"firmName\":\"2\",\"inDateTime\":1755883080000,\"acLevel\":52},{\"createdTime\":1755884280000,\"endTime\":0,\"firmName\":\"2\",\"inDateTime\":1755884280000,\"acLevel\":55},{\"createdTime\":1755886860000,\"endTime\":0,\"firmName\":\"2\",\"inDateTime\":1755886860000,\"acLevel\":86},{\"createdTime\":1755888660000,\"endTime\":0,\"firmName\":\"2\",\"inDateTime\":1755888660000,\"acLevel\":83},{\"createdTime\":1755892260000,\"endTime\":0,\"firmName\":\"2\",\"inDateTime\":1755892260000,\"acLevel\":90},{\"createdTime\":1755892860000,\"endTime\":0,\"firmName\":\"2\",\"inDateTime\":1755892860000,\"acLevel\":20},{\"createdTime\":1755924120000,\"endTime\":0,\"firmName\":\"2\",\"inDateTime\":1755924120000,\"acLevel\":52}]")

                if (originalListIt.isNullOrEmpty()) return@doOnIOThread

//                val listItStr = JsonUtils.toJson(originalListIt)

                var totalCalorie = 0L

                val groupedData = common48DataConvert(doSum = true,curTime=curTime, originalList=originalListIt)
                groupedData.forEach { beanIt->
                    totalCalorie += beanIt.mValue.toInt()
                }
                groupedData.forEach { groupedDataIt->
                    groupedDataIt.mValue = MathUtil.sub(groupedDataIt.mValue.toString(),"6").toFloat()
                }

                doOnUiThread {
                    // 在代码中设置数据
                    candlestickChart?.setData(groupedData)
                }
            }
        }
    }
}